package com.ciphergateway.dsre.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ciphergateway.dsre.entity.Comment;
import com.ciphergateway.dsre.mapper.CommentMapper;
import com.ciphergateway.dsre.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private ICommentService commentService;
    @Resource
    private CommentMapper commentMapper;

    // 获取所有评论
    @GetMapping
    public List<Comment> getAllComments() {
        return commentService.list();
    }

    // 根据ID获取单个评论

    @GetMapping("/{pageId}")
    public List<Comment> getCommentsByPageId(@PathVariable Integer pageId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("page_Id", pageId); // 假设你的字段是 page_id
        return commentService.list(queryWrapper); // 返回所有符合条件的记录
    }


    // 添加评论

    @PostMapping("/{pageId}")
    public void addComment(@RequestBody Comment comment,@PathVariable Integer pageId) {
       comment.setPageId(pageId);
        commentService.save(comment);
    }

    // 更新评论
    @PutMapping("/{id}")
    public void updateComment(@RequestBody Comment comment) {
        commentService.updateById(comment);
    }


    // 删除评论
    @DeleteMapping("/{id}")
    public void deleteComment(@PathVariable Integer id) {
        commentService.removeById(id);
    }


}
